package dao;

import domain.Page;
import domain.User;
import util.DButils;
import util.EncryptUtil;

import java.sql.*;
import java.util.ArrayList;

public class UserDao {
    int User_id;
    String Username=null;
    String Password=null;
    String View_power=null;
    int Verify_power;
    int Community_power;
    int Room_power;
    public User verify(String username, String password) {   //验证用户名密码
        User user=null;
        Connection conn=null;
        Statement statement =null;
        ResultSet rs=null;
        conn= DButils.getConn();
        EncryptUtil en =  new EncryptUtil();
        password = en.getMD5(password);
        try {
            statement=conn.createStatement();
            rs=statement.executeQuery("select* from user where Username='"+username+"'");
            while(rs.next()) {
                User_id=rs.getInt("User_id");
               Username=rs.getString("Username");
               Password=rs.getString("Password");
               View_power=rs.getString("View_power");
               Verify_power=rs.getInt("Verify_power");
               Community_power=rs.getInt("Community_power");
               Room_power=rs.getInt("Room_power");
                if(username.equals(Username)&&password.equals(Password))
                {
                    user=new User();
                    user.setUser_id(User_id);
                    user.setUsername(Username);
                    user.setView_power(View_power);
                    user.setVerify_power(Verify_power);
                    user.setCommunity_power(Community_power);
                    user.setRoom_power(Room_power);
                }
            }DButils.close(rs, statement, conn);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return user;
    }

    public int getCont() {  //获取除admin用户数量
        String sql = "select count(*) as count from user where Username not like 'admin'";
        Connection con= DButils.getConn();
        int count=0;
        try {
            Statement st = con.createStatement();
            ResultSet rs =st.executeQuery(sql);
            rs.next();
            count=rs.getInt("count");
            DButils.close(rs,st,con);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return count;
    }

    public ArrayList<User> findByPage(Page page) {     //分页查询user数据
        String sql = "select * from user where Username not like 'admin' limit " + page.getBeginIndex() + ", " + page.getEveryPage();
        ArrayList<User> list=new ArrayList<User>();
        Connection con = DButils.getConn();
        try {
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(sql);
            while (rs.next()){
                User user=new User();
                user.setUser_id(rs.getInt("User_id"));
                user.setUsername(rs.getString("Username"));
                user.setView_power(rs.getString("View_power"));
                user.setVerify_power(rs.getInt("Verify_power"));
                user.setCommunity_power(rs.getInt("Community_power"));
                user.setRoom_power(rs.getInt("Room_power"));
                list.add(user);
            }
            DButils.close(rs,st,con);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return  list;
    }

    public boolean VerifyUsername(String username){          //验证用户名唯一
        String sql="select count(*) as count from user where Username='"+username+"'";
        Connection con=DButils.getConn();
        int count=1;
        try {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            rs.next();
            count = rs.getInt("count");
            DButils.close(rs,st,con);
        }catch (SQLException e){
            e.printStackTrace();
        }
        if(count==0)
            return true;
        else
            return false;
    }

    public int CreatUser(String username,String password){ //创建新用户
        EncryptUtil en =new EncryptUtil();
        String sql = "insert into user (Username,Password,Verify_power,Community_power,Room_power) values('"+username+"','"+en.getMD5(password)+"',0,0,0)";
        Connection con = DButils.getConn();
        int i = 0;
        try {
            Statement st = con.createStatement();
            i = st.executeUpdate(sql);
            st.close();
            con.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return i;
    }

    public int UpdateUser(int User_id,String Password){   //修改用户密码
        EncryptUtil en = new EncryptUtil();
        String Passwordmd5= en.getMD5(Password);
        String sql = "update user set Password='"+Passwordmd5+"' where User_id="+User_id;
        Connection con = DButils.getConn();
        int i = 0;
        try{
            Statement st = con.createStatement();
            i = st.executeUpdate(sql);
            DButils.close(st,con);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return i;


    }

    public int UpdatePower(int Userid,String view_power,int verify_power,int community_power,int room_power){    //权限配置
        Connection conn = DButils.getConn();
        String sql = "update user set View_power='"+view_power+"',Verify_power="+verify_power+",Community_power="+community_power+",Room_power="+room_power+" where User_id="+Userid;
        int i = 0;
        try{
            Statement st = conn.createStatement();
            i = st.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return i;
    }
}
